package com.xingongchang.baolebei.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.activity.BaolebeiActivity;


public class GalleryImageAdapter extends PagerAdapter {

	private LayoutInflater mInflater;

	LayoutInflater inflater;

	Context mContext;
	int screenWidth;
	int screenHeight;
	private SharedPreferences shared;
	private SharedPreferences.Editor editor;

	public GalleryImageAdapter(Context context) {
		mInflater = LayoutInflater.from(context);
		inflater = LayoutInflater.from(context);
		mContext = context;
		shared = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
		editor = shared.edit();
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		screenWidth = wm.getDefaultDisplay().getWidth();// 屏幕宽度
		screenHeight = wm.getDefaultDisplay().getHeight();// 屏幕高度
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 3;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((View) object);
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}

	@Override
	public Object instantiateItem(ViewGroup view, int position) {

		final ViewHolder holder;

		holder = new ViewHolder();
		View imageLayout = mInflater.inflate(R.layout.gallery_image_item, null);

		holder.image = (LinearLayout) imageLayout
				.findViewById(R.id.gallery_image_item_view);


		if (position == 2) {
			holder.image.setEnabled(true);
		} else {
			holder.image.setEnabled(false);
		}
		if (position == 0) {
			holder.image.removeAllViews();
			View view0 = inflater.inflate(R.layout.lead_a, null);
			holder.image.addView(view0);
		} else if (position == 1) {
			holder.image.removeAllViews();
			View view1 = inflater.inflate(R.layout.lead_b, null);
			holder.image.addView(view1);
		} else if (position == 2) {
			holder.image.removeAllViews();
			View view2 = inflater.inflate(R.layout.lead_c, null);
			ImageView enter = (ImageView) view2.findViewById(R.id.btn_enter);
			enter.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					editor.putBoolean("isFirstRun", false);
					editor.commit();
					Intent intent = new Intent(mContext,BaolebeiActivity.class);
					mContext.startActivity(intent);
					((Activity)mContext).finish();
					
				}
			});
			holder.image.addView(view2);
		}

		((ViewPager) view).addView(imageLayout, 0);

		return imageLayout;
	}

	class ViewHolder {
		private LinearLayout image;
	}


}
